package org.mijia.dotsbox.rcp;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewFolderDialog extends TitleAreaDialog {
	
	private static final int DIALOG_MIN_WIDTH = 350;
	private Button btnOk;
	private Text input;
	private String folderName;
	
	private ChannelTreeModel treeModel;
	
	public NewFolderDialog(Shell parentShell, ChannelTreeModel model) {
		super(parentShell);
		treeModel = model;
	}

	@Override
    protected Control createDialogArea(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        layout.marginWidth = 5;
        layout.marginHeight = 10;
        layout.verticalSpacing = 5;
        composite.setLayout(layout);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        composite.setLayoutData(gd);
        
        setMessage("Please input the news folder name:",
                IMessageProvider.INFORMATION);
        
        Label titleLabel = new Label(composite, SWT.NONE);
        titleLabel.setText("Folder Name: ");
        gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
        titleLabel.setLayoutData(gd);
        
        input = new Text(composite, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        input.setLayoutData(gd);
        input.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                verifyFolderName();
            }
        });

        return super.createDialogArea(parent);
    }
	
    private void verifyFolderName() {
        folderName = input.getText().trim();
        if(folderName == null || folderName.length() == 0) {
            folderName = "";
            btnOk.setEnabled(false);
        } else {
        	if (treeModel.containsFolder(folderName)) {
                setMessage("We already have a folder called \"" + folderName + "\"",
                        IMessageProvider.ERROR);
                btnOk.setEnabled(false);
            } else {
                setMessage("Please input the news folder name:",
                        IMessageProvider.INFORMATION);
                btnOk.setEnabled(true);
            }
        }
    }
    
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("Create News Folder");
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        btnOk = createButton(parent, IDialogConstants.OK_ID, "&Ok", true);
        btnOk.setEnabled(false);
        Button btnCancel = createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", false);
    }

    @Override
    protected int getShellStyle() {
        int style = SWT.TITLE | SWT.APPLICATION_MODAL | SWT.BORDER | SWT.CLOSE | getDefaultOrientation();
        return style;
    }
    
    @Override
    protected void initializeBounds() {
        super.initializeBounds();
        Point bestSize = getShell().computeSize(
                convertHorizontalDLUsToPixels(DIALOG_MIN_WIDTH), SWT.DEFAULT);
        Point location = getInitialLocation(bestSize);
        getShell().setBounds(location.x, location.y, bestSize.x, bestSize.y);
    }

    @Override
    protected void setButtonLayoutData(Button button) {
        GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
        int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
        gd.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT,
                SWT.DEFAULT, true).x);
        button.setLayoutData(gd);
    }
    
    public String getFolderName() {
        return folderName;
    }
}
